﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ecommerce2011.Controllers
{
    public class OrderController : Controller
    {
        //
        // GET: /Order/

        [Authorize]
        public ActionResult Index()
        {
            String cliente = System.Web.HttpContext.Current.User.Identity.Name;
            OrdenesServiceProxy.wsOrdenesService srvc = new OrdenesServiceProxy.wsOrdenesService();
            OrdenesServiceProxy.ordersDTO[] ordenes = srvc.OrdenesAbiertasCliente(cliente);
            List<OrdenesServiceProxy.ordersDTO> listaOrdenes;
            if (ordenes != null)
                listaOrdenes = ordenes.ToList();
            else
                listaOrdenes = new List<OrdenesServiceProxy.ordersDTO>();
            return View(listaOrdenes);
        }

        //
        // GET: /Order/Details/5
        [Authorize]
        public ActionResult Details(string id)
        {
            String cliente = System.Web.HttpContext.Current.User.Identity.Name;
            OrdenesServiceProxy.wsOrdenesService srvc = new OrdenesServiceProxy.wsOrdenesService();
            OrdenesServiceProxy.ordersDTO[] ordenes = srvc.BuscarOrdenxNumero(id);
            List<OrdenesServiceProxy.itemsDTO> listaItems;
            if (ordenes != null)
                if (ordenes[0].itemsCollection != null)
                    listaItems = ordenes[0].itemsCollection.ToList();
                else
                    listaItems = new List<OrdenesServiceProxy.itemsDTO>();
            else
                listaItems = new List<OrdenesServiceProxy.itemsDTO>();
            return View(listaItems);
        }

        //
        // GET: /Order/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Order/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Order/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Order/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Order/Delete/5
        [Authorize]
        public ActionResult Delete(string id)
        {
            String cliente = System.Web.HttpContext.Current.User.Identity.Name;
            OrdenesServiceProxy.wsOrdenesService srvc = new OrdenesServiceProxy.wsOrdenesService();
            OrdenesServiceProxy.ordersDTO[] ordenes = srvc.BuscarOrdenxNumero(id);
            return View(ordenes[0]);
        }

        //
        // POST: /Order/Delete/5
        [Authorize]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            // TODO: Add delete logic here
            String cliente = System.Web.HttpContext.Current.User.Identity.Name;
            OrdenesServiceProxy.wsOrdenesService srvc = new OrdenesServiceProxy.wsOrdenesService();
            bool respuesta = srvc.CancelarOrden(id);
 
            return RedirectToAction("Index");
        }
    }
}
